using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using GameManager;
using GameManager.GamePlay;
using UnityEngine;
using UnityEngine.UIElements;

public class GuideLineScripts : MonoBehaviour
{
    public LineRenderer line1;
    public LineRenderer line2;
    public LineRenderer line3;
    public LineRenderer Circle;

    public LayerMask LayerMask;

    [Header("直线长度")]
    public float Line2Distance;
    [Header("垂直线长度")]
    public float Line3Distance;
    //碰撞点事件
    public Action<Vector3> EndPositionAction;
    [Header("目标球角度")]
    public float BetweenAngle;

    public GameObject Root;
    [HideInInspector] public Vector3 Line2DirectionNormalized; //其实就是Line2Direction
    [HideInInspector] public Vector3 Line3DirectionNormalized;  //其实就是Line3Direction

    public bool IsGuideLine;

    public Vector3 ShotDirection = Vector3.zero; //击球方向，根据射线方向决定
    
    private Ray ray = new Ray();

    private WhiteBall WhiteBall;
    private GameObject CueGameObject;
    public float radius;

    public float LineOffect;

    private bool IsInit = false;
    //圆形LineRender半径
    private float xradius;
    private float yradius;
    private int segments = 20;

    private bool IsOpen = false;

    private float line2PosLength;
    private float line3PosLength;
    private float Line2PosLengthCurrent;
    private float Line3PosLengthCurrent;
    public void Init()
    {
        WhiteBall = BiliardGameManager.instance.whiteBall;
        CueGameObject = BiliardGameManager.instance.CueRotGameObject;
        IsInit = true;
        xradius = WhiteBall.GetComponent<WhiteBall>().PhysicsRadius;
        yradius = xradius;
        CircleLineComponent circleLineComponent = new CircleLineComponent();
        circleLineComponent.CreatePoints(Circle,xradius,yradius);
        IsGuideLine = true;
    }
    // public SelfUI selfUI;
    public void OpenLine()
    {
        Root.gameObject.SetActive(true);
    }

    public void CloseLine()
    {
        Root.gameObject.SetActive(false);
    }

    private void Update()
    {
        if (IsInit)
        {
            DrawGuideLine();
            if (Line2PosLengthCurrent < line2PosLength)
            {
                Line2PosLengthCurrent += Time.deltaTime *1.5f;
                if (Line2PosLengthCurrent >= line2PosLength)
                {
                    Line2PosLengthCurrent = line2PosLength;
                }
            }

            if (Line2PosLengthCurrent > line2PosLength)
            {
                Line2PosLengthCurrent -= Time.deltaTime *1.5f;
                if (Line2PosLengthCurrent <= line2PosLength)
                {
                    Line2PosLengthCurrent = line2PosLength;
                }
            }

            if (Line3PosLengthCurrent < line3PosLength)
            {
                Line3PosLengthCurrent += Time.deltaTime *1.5f;
                if (Line2PosLengthCurrent >= line3PosLength)
                {
                    Line3PosLengthCurrent = line3PosLength;
                }
            }

            if (Line3PosLengthCurrent > line3PosLength)
            {
                Line3PosLengthCurrent -= Time.deltaTime *1.5f;
                if (Line3PosLengthCurrent <= line3PosLength)
                {
                    Line3PosLengthCurrent = line3PosLength;
                }
            }
        }

        // if (Input.GetKey(KeyCode.B))
        // {
        //     Vector3 direction = WhiteBall.transform.position - new Vector3(CueGameObject.transform.position.x,
        //         WhiteBall.transform.position.y,CueGameObject.transform.position.z);
        //     float radiusaaa = BiliardGameManager.Instance.radius;
        //     BEPUutilities.Ray ray = new BEPUutilities.Ray(WhiteBall.transform.position, direction);
        //     BEPUutilities.BoundingSphere boundingSphere = new BEPUutilities.BoundingSphere();
        //     boundingSphere.Center = WhiteBall.transform.position;
        //     boundingSphere.Radius = (Fix64)radiusaaa;
        //     Fix64 testaaa= 0;
        //     if (ray.Intersects(boundingSphere, out testaaa))
        //     {
        //         
        //     }
        // }
    }
    

    #region MyRegion
    
    private void DrawGuideLine()
    {
        if (!IsGuideLine)
        {
            return;
        }
        
        Vector3 direction = WhiteBall.transform.position - new Vector3(CueGameObject.transform.position.x,
            WhiteBall.transform.position.y,CueGameObject.transform.position.z);
    
        // BEPUutilities.Ray ray1 = new BEPUutilities.Ray();
        // ray1.Position = BiliardGameManager.Instance.WhiteBall.Sphere.Position;
        // ray1.Direction = direction * 1000;
        //
        // Fix64 outFix64;
        // if (ray1.Intersects(BiliardGameManager.Instance.targetBallScripst.targetBallList[0].Sphere.CollisionInformation.BoundingBox,out outFix64))
        // {
        //     Debug.LogError(outFix64);
        // }
    
        line1.SetPosition(0,WhiteBall.transform.position);
        ray.origin = WhiteBall.transform.position - direction.normalized * 0.001f;
        // ray.origin = BiliardGameManager.Instance.WhiteBall.transform.position;
        ray.direction = direction;
        
        radius = WhiteBall.PhysicsRadius - LineOffect;
        // 绘制射线
        RaycastHit rayHitInfo;
        bool rayCast = Physics.Raycast(ray,out rayHitInfo,LayerMask);
        RaycastHit Spherehitinfo;
        if (Physics.SphereCast(ray, radius, out Spherehitinfo, 300, LayerMask))
        {
            Vector3 endposition = ray.origin + (ray.direction.normalized * Spherehitinfo.distance);
            Vector3 endPosBEP = WhiteBall.transform.position
                                              + ray.direction.normalized *
                                                Spherehitinfo.distance;
            //    endposition -= ray.direction.normalized * radius; // 考虑球的半径调整
            EndPositionAction?.Invoke(endposition);
            //这一步是为了让直线是直的，因为碰撞检测使用的是一个球形盒子，可能导致碰撞地点不是直线

            line1.SetPosition(1, endposition + new Vector3(0,0.01f,0));
            ShotDirection = (endposition - WhiteBall.transform.position).normalized;
            if (ShotDirection == Vector3.zero)
            {
                ShotDirection = ray.direction.normalized;
            }
            BiliardGameManager.instance.GamePlayInfo.ShotDirection = ShotDirection;

            Vector3 bepVector3 = endPosBEP -
                                            WhiteBall.transform.position;
            bepVector3.Normalize();

            Circle.gameObject.SetActive(true);
            Circle.gameObject.transform.position = endposition;
            if (Spherehitinfo.transform.gameObject.layer == 7) //如果碰撞的物体是目标球，则打开角线
            {
                line2.transform.gameObject.SetActive(true);
                line3.transform.gameObject.SetActive(true);
                Line2DirectionNormalized = Spherehitinfo.transform.position - endposition;
                Vector3 Line2director = (Spherehitinfo.transform.position - endposition).normalized;
                line2.SetPosition(0, Spherehitinfo.transform.position + Line2DirectionNormalized.normalized * radius);

                Vector3 Line3Director = new Vector3(Line2director.z, WhiteBall.transform.position.y, -Line2director.x).normalized;
                // Vector3 HitDirector = (hitinfo.transform.position - WhiteBall.transform.position).normalized;
                Vector3 HitDirector = ray.direction.normalized;
                HitDirector = new Vector3(HitDirector.x, WhiteBall.transform.position.y, HitDirector.z);
                BetweenAngle = Vector3.Angle(HitDirector, Line3Director);
                BiliardGameManager.instance.GamePlayInfo.BetweenAngle = BetweenAngle;

                if (BetweenAngle > 90)
                {
                    Line3Director = new Vector3(-Line2director.z, WhiteBall.transform.position.y, Line2director.x).normalized;
                }
                // Debug.Log(BetweenAngle);
                line3.SetPosition(0, endposition + Line3DirectionNormalized.normalized * radius);
                float line3PosBetween = BetweenAngle > 90 ? 180 - BetweenAngle : BetweenAngle;
                line3PosLength = 1 - (line3PosBetween / 90);
                line2PosLength = 1 - line3PosLength;
                line2PosLength = Mathf.Max(min, line2PosLength);
                line3PosLength = Mathf.Max(min, line3PosLength);
                Vector3 line2Pos = new Vector3(Line2director.x * Line2Distance, 0.01f,
                    Line2director.z * Line2Distance );

                line2.SetPosition(1, Spherehitinfo.transform.position + Line2DirectionNormalized.normalized * radius + line2Pos);
                Vector3 line3Pos = new Vector3(Line3Director.x * Line3Distance, 0f,
                    Line3Director.z * Line3Distance); // Line3为什么不够长？

                line3.SetPosition(1, endposition + line3Pos);
                Line3DirectionNormalized = Line3Director.normalized;
                // BiliardGameManager.Instance.angleLine = Vector3.Distance(endposition, endposition + line3Pos);
                //  Debug.Log("长度   " + Vector3.Distance(endposition, endposition + line3Pos));
            }
            else
            {
                line2.transform.gameObject.SetActive(false);
                line3.transform.gameObject.SetActive(false);
                Line2DirectionNormalized = Vector3.zero;
                BetweenAngle = 0;
                BiliardGameManager.instance.GamePlayInfo.BetweenAngle = BetweenAngle;
            }
        }
        else
        {
            Circle.gameObject.SetActive(false);
        }
    }
    
    #endregion

    public float min=0.2f;
    
    //private void 
    
}
